﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using LinqToAmazonSL;
using System.Windows.Media.Imaging;
using System.Windows.Browser;
using SilverlightApplication.LanguageResources;

namespace SilverlightApplication.UIControls.Carousel
{
    public partial class ItemDisplayer : UserControl
    {
        ScaleTransform scaleTransform;
        AmazonItem displayingData;
        BitmapImage image;
        private String url = null;


        public AmazonItem DisplayingData
        {
            get { return displayingData; }
            set
            {
                displayingData = value;
                displayData(value);
            }
            
        }

        private void displayData(AmazonItem item)
        {
           

            if (item.GetType() == typeof(AmazonBook))
            {
                tblAuthor.Visibility = Visibility.Visible;
                tblTitle.Visibility = Visibility.Visible;
                tblIsbn.Visibility = Visibility.Visible;
                tbllist.Visibility = Visibility.Visible;
                tblcollectible.Visibility = Visibility.Visible;
                tblused.Visibility = Visibility.Visible;
                tblnew.Visibility = Visibility.Visible;
                tblpages.Visibility = Visibility.Visible;
                tblpublisherbook.Visibility = Visibility.Visible;
                author.Visibility = Visibility.Visible;
                isbn.Visibility = Visibility.Visible;
                list.Visibility = Visibility.Visible;
                collectible.Visibility = Visibility.Visible;
                used.Visibility = Visibility.Visible;
                newbook.Visibility = Visibility.Visible;
                pages.Visibility = Visibility.Visible;
                publisherbook.Visibility = Visibility.Visible;

                tblActor.Visibility = Visibility.Collapsed;
                tblDirector.Visibility = Visibility.Collapsed;
                tblPublisher.Visibility = Visibility.Collapsed;
                tbluseddvd.Visibility = Visibility.Collapsed;
                tblpriceDvd.Visibility = Visibility.Collapsed;
                tblcollectibledvd.Visibility = Visibility.Collapsed;
                actor.Visibility = Visibility.Collapsed;
                director.Visibility = Visibility.Collapsed;
                publisher.Visibility = Visibility.Collapsed;
                useddvd.Visibility = Visibility.Collapsed;
                listdvd.Visibility = Visibility.Collapsed;
                newdvd.Visibility = Visibility.Collapsed;
                collectibledvd.Visibility = Visibility.Collapsed;
                tblnewddvd.Visibility = Visibility.Collapsed;

                AmazonBook book = item as AmazonBook;
                if ((book.Author != null) && !book.Author.Equals(string.Empty))
                {
                    tblAuthor.SetValue(ToolTipService.ToolTipProperty, book.Author);
                    String autores = book.Author;
                    if (autores.Length > 30)
                    {
                        autores = autores.Insert(30, "...");
                        autores = autores.Substring(0, 33);
                    }
                    tblAuthor.Text = autores;
                }
                else
                {
                    tblAuthor.SetValue(ToolTipService.ToolTipProperty, "N/A");
                    tblAuthor.Text = "N/A";

                }
                if ((book.Title != null) && !book.Title.Equals(string.Empty))
                {
                    tblTitle.SetValue(ToolTipService.ToolTipProperty, book.Title);
                  
                    tblTitle.FontSize = 16;
                    String title = book.Title;
                    if (title.Length > 45)
                    {
                        title = title.Insert(45, "...");
                        title = title.Substring(0, 48);
                    }
                    tblTitle.Text = title;
                    tblTitle.TextAlignment = TextAlignment.Center;
                    tblTitle.Tag = book.Title;
                }
                else
                {
                    tblTitle.SetValue(ToolTipService.ToolTipProperty, "N/A");
                    tblTitle.Text = "N/A";
                }                  
          
                if ((book.DetailPage != null) && !book.DetailPage.Equals(string.Empty))
                {
                    url = book.DetailPage;
                }
                else
                {
                    url =  "http://www.amazon.com";
                }

               
                if ((book.ISBN != null) && !book.ISBN.Equals(string.Empty))
                {
                    tblIsbn.Text = book.ISBN;
                }
                else
                {
                    tblIsbn.Text = "N/A";
                }
                if ((book.Price != null) && !book.Price.Equals(string.Empty))
                {
                    tbllist.Text = book.Price;
                }
                else
                {
                    tbllist.Text = "N/A";
                }
                if ((book.LowestUsedPrice != null) && !book.LowestUsedPrice.Equals(string.Empty))
                {
                    tblused.Text = book.LowestUsedPrice;
                }
                else
                {
                    tblused.Text = "N/A";
                }
                if ((book.LowestNewPrice != null) && !book.LowestNewPrice.Equals(string.Empty))
                {
                    tblnew.Text = book.LowestNewPrice;
                }
                else
                {
                    tblnew.Text = "N/A";
                }
                if ((book.LowestCollectiblePrice != null) && !book.LowestCollectiblePrice.Equals(string.Empty))
                {
                    tblcollectible.Text = book.LowestCollectiblePrice;
                }
                else
                {
                    tblcollectible.Text = "N/A";
                }
                if ((book.NumberOfPages != null) && !book.NumberOfPages.Equals(string.Empty))
                {
                    tblpages.Text = book.NumberOfPages;
                }
                else
                {
                    tblpages.Text = "N/A";
                }
                if ((book.Publisher != null) && !book.Publisher.Equals(string.Empty))
                {
                    tblpublisherbook.SetValue(ToolTipService.ToolTipProperty, book.Publisher);
                    String pubs = book.Publisher;
                    if (pubs.Length > 30)
                    {
                        pubs = pubs.Insert(30, "...");
                        pubs = pubs.Substring(0, 33);
                    }
                    tblpublisherbook.Text = pubs;
                }
                else
                {
                    tblpublisherbook.SetValue(ToolTipService.ToolTipProperty, "N/A");
                    tblpublisherbook.Text = "N/A";
                }


                if (book.Image != null && book.Image.URL != null && !book.Image.URL.Equals(string.Empty))
                {
                  
                    ImageBrush imageBrush = new ImageBrush();
                    imageBrush.ImageSource = new BitmapImage(new Uri(book.Image.URL, UriKind.RelativeOrAbsolute));
                    re1.Fill = imageBrush;
                    re2.Fill = imageBrush;
                    
                }
                else
                {
                    ImageBrush imageBrush = new ImageBrush();
                    imageBrush.ImageSource = new BitmapImage(new Uri("/SilverlightApplication;component/UIControls/Carousel/Resources/book_no_image.jpg", UriKind.RelativeOrAbsolute));
                    re1.Fill = imageBrush;
                    re2.Fill = imageBrush;
                }
            }
            else
            {
                tblActor.Visibility = Visibility.Visible;
                tblTitle.Visibility = Visibility.Visible;
                tblDirector.Visibility = Visibility.Visible;
                tblPublisher.Visibility = Visibility.Visible;
                tbluseddvd.Visibility = Visibility.Visible;
                tblpriceDvd.Visibility = Visibility.Visible;
                tblcollectibledvd.Visibility = Visibility.Visible;
                actor.Visibility = Visibility.Visible;
                director.Visibility = Visibility.Visible;
                publisher.Visibility = Visibility.Visible;
                useddvd.Visibility = Visibility.Visible;
                listdvd.Visibility = Visibility.Visible;
                newdvd.Visibility = Visibility.Visible;
                collectibledvd.Visibility = Visibility.Visible;
                tblnewddvd.Visibility = Visibility.Visible;

                tblAuthor.Visibility = Visibility.Collapsed;
                tblIsbn.Visibility = Visibility.Collapsed;
                tbllist.Visibility = Visibility.Collapsed;
                tblcollectible.Visibility = Visibility.Collapsed;
                tblused.Visibility = Visibility.Collapsed;
                tblnew.Visibility = Visibility.Collapsed;
                tblpages.Visibility = Visibility.Collapsed;
                author.Visibility = Visibility.Collapsed;
                isbn.Visibility = Visibility.Collapsed;
                list.Visibility = Visibility.Collapsed;
                collectible.Visibility = Visibility.Collapsed;
                used.Visibility = Visibility.Collapsed;
                newbook.Visibility = Visibility.Collapsed;
                pages.Visibility = Visibility.Collapsed;
                tblpublisherbook.Visibility = Visibility.Collapsed;
                publisherbook.Visibility = Visibility.Collapsed;

                AmazonDVD dvd = item as AmazonDVD;
                if ((dvd.Actor != null) && !dvd.Actor.Equals(string.Empty))
                {
                    tblActor.SetValue(ToolTipService.ToolTipProperty, dvd.Actor);
                    String actores = dvd.Actor;
                    if (actores.Length > 30)
                    {
                        actores = actores.Insert(30, "...");
                        actores = actores.Substring(0, 33);
                    }
                    tblActor.Text = actores;
                   
                }
                else
                {
                    tblActor.SetValue(ToolTipService.ToolTipProperty, "N/A");
                    tblActor.Text = "N/A";
                }
                if ((dvd.Title != null) && !dvd.Title.Equals(string.Empty))
                {
                    tblTitle.SetValue(ToolTipService.ToolTipProperty, dvd.Title);
                   
                    tblTitle.FontSize = 16;
                    String title = dvd.Title;
                    if (title.Length > 45)
                    {
                        title = title.Insert(45, "...");
                        title = title.Substring(0, 48);
                    }
                    tblTitle.Text = title;
                    tblTitle.TextAlignment = TextAlignment.Center;
                    tblTitle.Tag = dvd.Title;
                }
                else
                {
                    tblTitle.SetValue(ToolTipService.ToolTipProperty, "N/A");
                    tblTitle.Text = "N/A";
                }

                if ((dvd.DetailPage != null) && !dvd.DetailPage.Equals(string.Empty))
                {
                    url = dvd.DetailPage;
                }
                else
                {
                    url = "http://www.amazon.com";
                }

              
                if ((dvd.Price != null) && !dvd.Price.Equals(string.Empty))
                {
                    tblpriceDvd.Text = dvd.Price;
                }
                else
                {
                    tblpriceDvd.Text = "N/A";
                }
                if ((dvd.LowestUsedPrice != null) && !dvd.LowestUsedPrice.Equals(string.Empty))
                {
                    tbluseddvd.Text = dvd.LowestUsedPrice;
                }
                else
                {
                    tbluseddvd.Text = "N/A";
                }
                if ((dvd.LowestNewPrice != null) && !dvd.LowestNewPrice.Equals(string.Empty))
                {
                    tblnewddvd.Text = dvd.LowestNewPrice;
                }
                else
                {
                    tblnewddvd.Text = "N/A";
                }
                if ((dvd.LowestCollectiblePrice != null) && !dvd.LowestCollectiblePrice.Equals(string.Empty))
                {
                    tblcollectibledvd.Text = dvd.LowestCollectiblePrice;
                }
                else
                {
                    tblcollectibledvd.Text = "N/A";
                }


                if (dvd.Image != null && dvd.Image.URL != null && !dvd.Image.URL.Equals(string.Empty))
                {

                    ImageBrush imageBrush = new ImageBrush();
                    imageBrush.ImageSource = new BitmapImage(new Uri(dvd.Image.URL, UriKind.RelativeOrAbsolute));
                    re1.Fill = imageBrush;
                    re2.Fill = imageBrush;

                }
                else
                {
                    ImageBrush imageBrush = new ImageBrush();
                    imageBrush.ImageSource = new BitmapImage(new Uri("/SilverlightApplication;component/UIControls/Carousel/Resources/dvd_no_image.jpg", UriKind.RelativeOrAbsolute));
                    re1.Fill = imageBrush;
                    re2.Fill = imageBrush;
                }
                if (dvd.Director != null && !dvd.Director.Equals(string.Empty))
                {
                    tblDirector.SetValue(ToolTipService.ToolTipProperty, dvd.Director);
                    String directores = dvd.Director;
                    if (directores.Length > 30)
                    {
                        directores = directores.Insert(30, "...");
                        directores = directores.Substring(0, 33);
                    }
                    tblDirector.Text = directores;

                }
                else
                {
                    tblDirector.SetValue(ToolTipService.ToolTipProperty, "N/A");
                    tblDirector.Text = "N/A";
                }
                if (dvd.Publisher != null && !dvd.Publisher.Equals(string.Empty))
                {
                    tblPublisher.SetValue(ToolTipService.ToolTipProperty, dvd.Publisher);
                    String editores = dvd.Publisher;
                    if (editores.Length > 30)
                    {
                        editores = editores.Insert(30, "...");
                        editores = editores.Substring(0, 33);
                    }
                    tblPublisher.Text = editores;
                }
                else
                {
                    tblPublisher.SetValue(ToolTipService.ToolTipProperty, "N/A");
                    tblPublisher.Text = "N/A";
                }
            
            }

        }

        public ItemDisplayer()
        {
            InitializeComponent();
            scaleTransform = new ScaleTransform();
            scaleTransform.CenterX = Width / 2;
            scaleTransform.CenterY = Height / 2;
            this.RenderTransform = scaleTransform;

            displayingData = null;
            image = new BitmapImage();
            //imgCover.Source = image;
            actor.Text = LanguageLoader.GetString("actor");
            director.Text = LanguageLoader.GetString("director");
            publisher.Text = LanguageLoader.GetString("publisher");
            listdvd.Text = LanguageLoader.GetString("listdvd");
            useddvd.Text = LanguageLoader.GetString("useddvd");
            newdvd.Text = LanguageLoader.GetString("newdvd");
            collectibledvd.Text = LanguageLoader.GetString("collectibledvd");
            author.Text = LanguageLoader.GetString("author");
            isbn.Text = LanguageLoader.GetString("isbn");
            publisherbook.Text = LanguageLoader.GetString("publisherbook");
            list.Text = LanguageLoader.GetString("list");
            used.Text = LanguageLoader.GetString("used");
            newbook.Text = LanguageLoader.GetString("newbook");
            collectible.Text = LanguageLoader.GetString("collectible");
            pages.Text = LanguageLoader.GetString("pages");
        }
       
            
       

        public double DisplayZoomFactor
        {
            get
            {
                return scaleTransform.ScaleX; // or ScaleY
            }
            set
            {
                scaleTransform.ScaleX = value;
                scaleTransform.ScaleY = value;
            }
        }

        private void tblTitle_MouseEnter(object sender, MouseEventArgs e)
        {
           
            TextDecorationCollection myUnderline = TextDecorations.Underline;
            tblTitle.TextDecorations = myUnderline;
            Border border = new Border();
            border.BorderBrush = bshBlue;
            border.BorderThickness = new Thickness(2.0);

            StackPanel myStackPanel = new StackPanel();
            myStackPanel.Background = new SolidColorBrush(Colors.LightGray);

            TextBlock tb = new TextBlock();
            String title = (String)tblTitle.Tag;
            tb.Text = title;
            tb.FontFamily = new FontFamily("Courier New");
            tb.FontSize = 12.0;
            tb.Foreground = bshBlue;
            tb.HorizontalAlignment = HorizontalAlignment.Center;
            tb.VerticalAlignment = VerticalAlignment.Center;
            
            myStackPanel.Children.Add(tb);
            border.Child = myStackPanel;
        }

        private void tblTitle_MouseLeave(object sender, MouseEventArgs e)
        {
           
            tblTitle.TextDecorations = null;
        }

        private void tblTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri(url, UriKind.Absolute), "_blank");
        }
    }
}